package org.jeecg.modules.htxincailiao.ability.controller;

import java.util.Arrays;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilityCategory;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilitySupply;
import org.jeecg.modules.htxincailiao.ability.service.IHTAbilityCategoryService;
import org.jeecg.modules.htxincailiao.ability.service.IHTAbilitySupplyService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.permissions.HTRolePermission;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ht_ability_supply
 * @Author: jeecg-boot
 * @Date:   2021-11-13 22:13:04
 * @Version: V1.0
 */
@Api(tags="ht_ability_supply")
@RestController
@RequestMapping("/ability/hTAbilitySupply")
@Slf4j
public class HTAbilitySupplyController extends JeecgController<HTAbilitySupply, IHTAbilitySupplyService> {
	@Resource
	private IHTAbilitySupplyService hTAbilitySupplyService;

	@Resource
	private IHTAbilityCategoryService abilityCategoryService;

	/**
	 * 分页列表查询
	 *
	 * @param hTAbilitySupply
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_ability_supply-分页列表查询")
	@ApiOperation(value="ht_ability_supply-分页列表查询", notes="ht_ability_supply-分页列表查询")
	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryPageList(HTAbilitySupply hTAbilitySupply,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				hTAbilitySupply = JSONObject.parseObject(line, HTAbilitySupply.class);

				pageNo = oConvertUtils.isNotEmpty(hTAbilitySupply.getPageNo()) ? hTAbilitySupply.getPageNo() : pageNo;
				pageSize = oConvertUtils.isNotEmpty(hTAbilitySupply.getPageSize()) ? hTAbilitySupply.getPageSize() : pageSize;
			}
		}

		if(oConvertUtils.isNotEmpty(hTAbilitySupply) &&
				oConvertUtils.isNotEmpty(hTAbilitySupply.getCategoryId()) &&
				hTAbilitySupply.getCategoryId().equals("0")) {
			//全部分类
			hTAbilitySupply.setCategoryId(null)	;
		}

		HTRolePermission.AddAbilitySupplyPermission(req, hTAbilitySupply);

		QueryWrapper<HTAbilitySupply> queryWrapper = QueryGenerator.initQueryWrapper(hTAbilitySupply, req.getParameterMap());
		queryWrapper.orderByDesc("update_time");
		Page<HTAbilitySupply> page = new Page<HTAbilitySupply>(pageNo, pageSize);
		IPage<HTAbilitySupply> pageList = hTAbilitySupplyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 分页列表查询
	 *
	 * @param hTAbilitySupply
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_ability_supply-分页列表查询")
	@ApiOperation(value="ht_ability_supply-分页列表查询", notes="ht_ability_supply-分页列表查询")
	@RequestMapping(value = "/pc-home-list", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryPCHomePageList(HTAbilitySupply hTAbilitySupply,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				hTAbilitySupply = JSONObject.parseObject(line, HTAbilitySupply.class);

				pageNo = oConvertUtils.isNotEmpty(hTAbilitySupply.getPageNo()) ? hTAbilitySupply.getPageNo() : pageNo;
				pageSize = oConvertUtils.isNotEmpty(hTAbilitySupply.getPageSize()) ? hTAbilitySupply.getPageSize() : pageSize;
			}
		}

		if(oConvertUtils.isNotEmpty(hTAbilitySupply) &&
				oConvertUtils.isNotEmpty(hTAbilitySupply.getCategoryId()) &&
				hTAbilitySupply.getCategoryId().equals("0")) {
			//全部分类
			hTAbilitySupply.setCategoryId(null)	;
		}

		HTRolePermission.AddAbilitySupplyPermission(req, hTAbilitySupply);

		QueryWrapper<HTAbilitySupply> queryWrapper = QueryGenerator.initQueryWrapper(hTAbilitySupply, req.getParameterMap());
		queryWrapper.orderByDesc("is_home_recommended").orderByDesc("update_time");

		Page<HTAbilitySupply> page = new Page<HTAbilitySupply>(pageNo, pageSize);
		IPage<HTAbilitySupply> pageList = hTAbilitySupplyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hTAbilitySupply
	 * @return
	 */
	@AutoLog(value = "ht_ability_supply-添加")
	@ApiOperation(value="ht_ability_supply-添加", notes="ht_ability_supply-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HTAbilitySupply hTAbilitySupply) {
		hTAbilitySupplyService.save(hTAbilitySupply);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hTAbilitySupply
	 * @return
	 */
	@AutoLog(value = "ht_ability_supply-编辑")
	@ApiOperation(value="ht_ability_supply-编辑", notes="ht_ability_supply-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HTAbilitySupply hTAbilitySupply) {
		hTAbilitySupplyService.updateById(hTAbilitySupply);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_ability_supply-通过id删除")
	@ApiOperation(value="ht_ability_supply-通过id删除", notes="ht_ability_supply-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hTAbilitySupplyService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ht_ability_supply-批量删除")
	@ApiOperation(value="ht_ability_supply-批量删除", notes="ht_ability_supply-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hTAbilitySupplyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_ability_supply-通过id查询")
	@ApiOperation(value="ht_ability_supply-通过id查询", notes="ht_ability_supply-通过id查询")
	@RequestMapping(value = "/queryById", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryById(HttpServletRequest req, @RequestParam(name="id",required=false) String id) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				HTAbilitySupply hTAbilitySupply  = JSONObject.parseObject(line, HTAbilitySupply.class);
				id = hTAbilitySupply.getId();
			}
		}

		HTAbilitySupply hTAbilitySupply = hTAbilitySupplyService.getById(id);
		if (oConvertUtils.isNotEmpty(hTAbilitySupply) &&
				oConvertUtils.isNotEmpty(hTAbilitySupply.getCategoryId())){
			HTAbilityCategory category = abilityCategoryService.getById(hTAbilitySupply.getCategoryId());
			if (oConvertUtils.isNotEmpty(category) && oConvertUtils.isNotEmpty(category.getName())){
				hTAbilitySupply.setCategoryName(category.getName());
			}
		}

		if(hTAbilitySupply==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hTAbilitySupply);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param hTAbilitySupply
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HTAbilitySupply hTAbilitySupply) {
		return super.exportXls(request, hTAbilitySupply, HTAbilitySupply.class, "ht_ability_supply");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, HTAbilitySupply.class);
	}

}
